package health

import (
	"gitee.com/g-qs/gin-vue-admin/server/global"
	"gitee.com/g-qs/gin-vue-admin/server/model/common/response"
	"gitee.com/g-qs/gin-vue-admin/server/model/health"
	healthReq "gitee.com/g-qs/gin-vue-admin/server/model/health/request"
	"gitee.com/g-qs/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"time"
)

type CircleApi struct {
}

// Publish 发布健康圈
// @Tags User
// @Summary 注册
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body circle.Publish true "发布健康圈"
// @Success 200 {string} string "{"code":0,"data":{},"msg":"发布成功"}"
// @Router /api/circle/publish [post]
func (circleApi *CircleApi) Publish(c *gin.Context) {
	var cc healthReq.Circle
	uid := utils.GetUserID(c)
	err := c.ShouldBindJSON(&cc)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if cc.Tid == 0 {
		response.FailWithDetailed("", "话题ID不能为空", c)
		return
	}
	a := utils.CreateAudit(global.GVA_CONFIG.Audit.Objs)
	if cc.Text != "" {
		TextStatus, _ := a.AuditText(cc.Text)
		if TextStatus == false {
			response.FailWithMessage("善语结善缘,恶言伤人心", c)
			return
		}
	}
	if cc.Img != "" {
		ImgStatus, _ := a.AuditImageUrl(cc.Img)
		if ImgStatus == false {
			response.FailWithMessage("请上传正规图片", c)
			return
		}
	}
	circleData := &health.Circle{
		Uid:  uid,
		Tid:  cc.Tid,
		Text: cc.Text,
		Img:  cc.Img,
	}
	err = CircleService.CreateCircle(circleData)
	if err != nil {
		response.FailWithDetailed("", err.Error(), c)
		return
	}
	// 添加评论积分相关
	err = UserService.AddPoints(uid, 2, 4)
	response.Result(200, "", "发表成功", c)
}

// Comment 评论健康圈
// @Tags User
// @Summary 注册
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body circle.Comment true "评论"
// @Success 200 {string} string "{"code":0,"data":{},"msg":"发布成功"}"
// @Router /api/circle/comment [post]
func (circleApi *CircleApi) Comment(c *gin.Context) {
	var cc healthReq.Comment
	uid := utils.GetUserID(c)
	err := c.ShouldBindJSON(&cc)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if cc.Cid == 0 {
		response.FailWithDetailed("", "健康圈ID不能为空", c)
		return
	}
	if cc.Text == "" {
		response.FailWithDetailed("", "评论不能为空", c)
		return
	}
	a := utils.CreateAudit(global.GVA_CONFIG.Audit.Objs)
	TextStatus, _ := a.AuditText(cc.Text)
	if TextStatus == false {
		response.FailWithMessage("善语结善缘,恶言伤人心", c)
		return
	}
	commentData := &health.CircleComment{
		Uid:     uid,
		ReplyID: cc.ReplyId,
		Text:    cc.Text,
		ToUid:   cc.ToUid,
		Cid:     cc.Cid,
	}
	err = CircleService.CreateCircleComment(commentData)
	if err != nil {
		response.FailWithDetailed("", err.Error(), c)
		return
	}
	// 添加评论积分相关
	err = UserService.AddPoints(uid, 2, 2)
	response.Result(200, "", "评论成功", c)
}

// GetCircleList 分页获取健康圈列表
// @Tags Branch
// @Summary 分页获取Branch列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structureReq.BranchSearch true "分页获取健康圈列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /circle/getCircleList [post]
func (circleApi *CircleApi) GetCircleList(c *gin.Context) {
	var pageInfo healthReq.ReqCircle
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, err := CircleService.GetCircleInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, list, "获取成功", c)
	}
}

// GetHealthCircleList 分页获取健康圈列表【管理端】
// @Tags Branch
// @Summary 分页获取Branch列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structureReq.BranchSearch true "分页获取健康圈列表【管理端】"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /circle/getHealthCircleList [get]
func (circleApi *CircleApi) GetHealthCircleList(c *gin.Context) {
	var pageInfo healthReq.HealthCircleSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := CircleService.GetHealthCircleList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// FindCircleInfo 获取健康圈详情
// @Tags Branch
// @Summary 分页获取Branch列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structureReq.BranchSearch true "分页获取Branch列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /circle/findCircleInfo [get]
func (circleApi *CircleApi) FindCircleInfo(c *gin.Context) {
	id := c.Query("c_id")
	Cid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if circle, err := CircleService.FindCircleInfo(uint(Cid), userID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, circle, "获取成功", c)
	}
}

// GetCircleCommentsInfoList 获取健康圈评论列表
// @Tags Branch
// @Summary 分页获取Branch列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structureReq.BranchSearch true "分页获取Branch列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /circle/getCircleCommentsList [post]
func (circleApi *CircleApi) GetCircleCommentsInfoList(c *gin.Context) {
	var reqComments healthReq.ReqComments
	err := c.ShouldBindJSON(&reqComments)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, err := CircleService.GetCircleCommentsInfoList(reqComments); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, list, "获取成功", c)
	}
}

// GetCircleCommentsList 获取评论列表
// @Tags Branch
// @Summary 健康圈
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structureReq.BranchSearch true "分页获取评论列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /circle/getCommentsList [post]
func (circleApi *CircleApi) GetCircleCommentsList(c *gin.Context) {
	var pageInfo healthReq.CommentsSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := CircleService.GetCircleCommentsList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// ReportCircle 举报健康圈
// @Tags Branch
// @Summary 分页获取Branch列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query structureReq.BranchSearch true "分页获取Branch列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /circle/reportCircle [post]
func (circleApi *CircleApi) ReportCircle(c *gin.Context) {
	uid := utils.GetUserID(c)
	var hc healthReq.ReqReport
	err := c.ShouldBindJSON(&hc)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	reportData := &health.CircleReportRecord{
		ID:        hc.Cid,
		Uid:       uid,
		CreatedAt: time.Time{},
	}
	err = CircleService.CreateCircleReportRecord(reportData)
	if err != nil {
		response.FailWithDetailed("", err.Error(), c)
		return
	}
	response.Result(200, "", "举报成功", c)
}

// CircleLike 健康圈点赞
// @Tags CircleLike
// @Summary 用id查询FdArticleAudio
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /circle/like [get]
func (circleApi *CircleApi) CircleLike(c *gin.Context) {
	id := c.Query("c_id")
	cid, _ := strconv.Atoi(id)
	userID := utils.GetUserID(c)
	if circleLike, err := CircleService.CircleLike(userID, uint(cid)); err != nil {
		global.GVA_LOG.Error("点赞失败!", zap.Error(err))
		response.FailWithMessage("点赞失败", c)
	} else {
		// 添加点赞积分相关
		err = UserService.AddPoints(userID, 1, 1)
		response.Result(200, circleLike, "点赞成功", c)
	}
}

// DeleteCircle 删除健康圈
// @Tags DeleteCircle
// @Summary 删除健康圈
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /circle/deleteCircle [get]
func (circleApi *CircleApi) DeleteCircle(c *gin.Context) {
	var hc health.Circle
	err := c.ShouldBindJSON(&hc)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := CircleService.DeleteCircle(hc); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteComment 删除评论
// @Tags DeleteComment
// @Summary 删除健康圈
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /circle/deleteComment [delete]
func (circleApi *CircleApi) DeleteComment(c *gin.Context) {
	var hc health.CircleComment
	err := c.ShouldBindJSON(&hc)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := CircleService.DeleteComment(hc); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}
